/**
 * Created with IntelliJ IDEA.
 * Description:阻塞队列  ArrayBlockQueue
 * User: 东莞呵呵
 * Date:2022-09-22
 * Time:19:37
 */

class MyBlockQueue{
    private int[] elem=new int[2];
    private int head=0;
    private int tail=0;
    private int size=0;

    public void put(int data) throws InterruptedException {
        synchronized (this){
            if(this.size==this.elem.length){
                this.wait();
            }
            this.elem[tail++]=data;
            this.size++;
            if(this.tail>=this.elem.length){
                this.tail=0;
            }
            this.notify();
        }
    }
    public int take() throws InterruptedException {
        synchronized (this){
            if(this.size==0){
                this.wait();
            }
            int ret=this.elem[head++];
            this.size--;
            if(this.head>=this.elem.length){
                this.head=0;
            }
            this.notify();
            return ret;
        }

    }
}


public class Demo14 {
    public static void main(String[] args) throws InterruptedException {
        MyBlockQueue queue=new MyBlockQueue();
        System.out.println(queue.take());
        queue.put(1);

        queue.put(2);
        System.out.println(queue.take());
        queue.put(3);
        System.out.println(queue.take());
        queue.put(4);
        System.out.println(queue.take());
        queue.put(5);
        System.out.println(queue.take());

    }
}
